package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.databinding.SearchViewAllBinding;
import sz.szsmk.citizencard.model.network.bean.response.SimpleRes;
import sz.szsmk.citizencard.ui.activity.SearchAllActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;
import sz.szsmk.citizencard.util.Utils;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/11 10:15
 * description:
 */
public class SearchAllAdapter extends BaseRecyclerAdapter<SearchAllAdapter.SearchAllHolder> {
    private SearchAllActivity mContext;
    private List<SimpleRes.ResultBean> viewData;
    private SearchTypeAdapter searchAllAdapter;
    private int type;
    private int page;
    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, double need_header);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public SearchAllAdapter(SearchAllActivity searchAllActivity) {
        mContext = searchAllActivity;
    }

//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.search_view_all;
//    }

    @Override
    public SearchAllHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return SearchAllHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(SearchAllHolder holder, int position) {
        loadItemData(position, holder.binding);

    }

    private void loadItemData(final int position, SearchViewAllBinding binding) {
        if (viewData != null) {

            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            binding.allViewRv.setLayoutManager(manager);
            searchAllAdapter = new SearchTypeAdapter(mContext);
            binding.allViewRv.setAdapter(searchAllAdapter);
            binding.allViewTitle.setText(viewData.get(position).getType_name());
            final List<SimpleRes.ResultBean.DetailBean> detailBeans = viewData.get(position).getDetail();
            if ((detailBeans == null || detailBeans.size() == 0)||page!=1) {
                binding.allViewTitle.setVisibility(View.GONE);
            }
            final int typeid = viewData.get(position).getKey_type();
            if (typeid == 2 || typeid == 3) {
                type = 0;
            } else {
                type = 1;
            }

            searchAllAdapter.setViewData(detailBeans, type, typeid);
            searchAllAdapter.notifyDataSetChanged();
            searchAllAdapter.setBaseInterface(new SearchTypeAdapter.BaseInterface() {
                @Override
                public void onClick(View view, int posi) {
                    SimpleRes.ResultBean resultBean = viewData.get(position);
                    SimpleRes.ResultBean.DetailBean detailBean = detailBeans.get(posi);
                    String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
                    String detail_id = detailBean.getDetail_id();
                    String name = detailBean.getName();
                    String lat = detailBean.getLat();
                    String lng = detailBean.getLng();
                    String link_url = detailBean.getLink_url();
                    String sys_label = detailBean.getSys_label();
                    double need_header = detailBean.getNeed_header();
                    double mLat = StringUtils.convertToDouble(lat, 0);
                    double mLng = StringUtils.convertToDouble(lng, 0);
                    if (typeid == 2) {
                        //网点
//                        Toast.makeText(mContext, detailBeans.get(posi).getName(), Toast.LENGTH_SHORT).show();
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", Integer.parseInt(detail_id));
                        bundle.putDouble("latitude", mLat);
                        bundle.putDouble("longitude", mLng);
                        JumpUtils.jumptoNatvie(mContext, "netdetail", 0, bundle);
                    }
                    if (typeid == 3) {

                        Utils.startSearchMap(mContext, mLat, mLng, name);
                    }
                    if (typeid == 4) {
                        String url = Constant.BASE_URL + "app-front-end/#/card/goSpotInfoApp?access_token=" + access_token + "&spot_id=" + detail_id;
                        Bundle bundle = new Bundle();
                        bundle.putString(Constant.TITLE, "");
                        bundle.putString("url", url);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                    if (typeid == 1) {

                        if (baseInterface != null) {
                            baseInterface.onClick(view, link_url, sys_label, need_header);
                        }
                    }
                }
            });

        }


        if (viewData != null) {
        }

    }



    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<SimpleRes.ResultBean> viewData, int type,int page) {
        this.viewData = viewData;
        this.type = type;
        this.page = page;
    }

    public static class SearchAllHolder extends BaseHolder {

        private SearchViewAllBinding binding;

        public static SearchAllHolder creat(ViewGroup parent) {
            SearchViewAllBinding binding = SearchViewAllBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new SearchAllHolder(binding);
        }

        public SearchAllHolder(SearchViewAllBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
